/*
Kakine
ActionScript Framework
Copyright © 2011, PUNY, LLC.

This file is part of KAKINE.

KAKINE is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

KAKINE is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with KAKINE.  If not, see <http://www.gnu.org/licenses/>.
*/ 
package com.puny.mvc.controllers.platformer
{
	import com.puny.mvc.controllers.platformer.adjuster.IAdjuster;
	import com.puny.mvc.views.View;
	import com.puny.mvc.views.platformer.EnemyView;
	
	/**
	 * $Revision: 706 $
	 * EnemyController
	 */
	public class EnemyController extends CharacterController
	{
		private var _mainCharacter:IMainCharacterController;
		private var _xml:XML
		private var _enemy:EnemyView;
		private var _adjuster:IAdjuster;

		public function EnemyController(value:IAdjuster) {
			_adjuster = value;
			super();
		}

		public function get adjuster():IAdjuster {
			return _adjuster;
		}
		public function set config(value:XML):void {
			_xml = value;
		}
		
		public function get config():XML {
			return _xml;
		}
		
		public function faceLeft():void {
			_enemy.scaleX=1;
			_enemy.x = 0;
		}
		
		public function faceRight():void {
			_enemy.scaleX=-1;
			_enemy.x = _enemy.width;
		}
		
		public function attack():void {
			_enemy.attack();
		}
		
		public function snarl():void {
			
		}
		
		protected function get erin():IMainCharacterController {
			return _mainCharacter;
		}
		
		override protected function setup():void {
			_enemy = new EnemyView(_xml.@url, _xml.@name);
			initialize(_xml.@x,_xml.@y);
			addChild(_enemy);
			super.setup();
		}
		
		public function get asset():EnemyView {
			return _enemy.parent != null ? _enemy: null;
		}

		override public function beforeReady():void {
			var ctrl:PlatformerStageController = parent as PlatformerStageController;
			_mainCharacter = ctrl.mainCharacter;
			_adjuster.face();
			_adjuster.mainCharacter = _mainCharacter;
			super.beforeReady();
		}

		override public function procedureOpen():Boolean {
			return !isOnGround();
		}

		override public function afterOpen():void {
			if (_adjuster != null) {
				_adjuster.initialize(groundItem);
			}
			super.afterOpen();
		}

		override protected function collisionAtWall(item:View):void {
			if (_adjuster != null) {
				_adjuster.bound(item);
			}
		}
		
		override public function procedureMainFrame():Boolean {
			return _adjuster.procedure();
		}
		override public function beforeClose():void {
			super.beforeClose();
			_mainCharacter = null;
		}
		override public function afterClose():void {
			super.afterClose();
			_adjuster.close();
			_xml = null;
			_enemy = null;
			_adjuster = null;
		}
	}
}